package com.quality.framework.web.service;

import com.quality.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.quality.common.constant.Constants;
import com.quality.common.constant.QualitygGroupConstants;
import com.quality.common.core.domain.entity.SysQualitygGroup;
import com.quality.common.core.domain.model.RegisterBody;
import com.quality.common.core.redis.RedisCache;
import com.quality.common.exception.QualitygGroup.CaptchaException;
import com.quality.common.exception.QualitygGroup.CaptchaExpireException;
import com.quality.common.utils.MessageUtils;
import com.quality.common.utils.SecurityUtils;
import com.quality.common.utils.StringUtils;
import com.quality.framework.manager.AsyncManager;
import com.quality.framework.manager.factory.AsyncFactory;
import com.quality.system.service.ISysConfigService;
import com.quality.system.service.ISysQualitygGroupService;

/**
 * 注册校验方法
 * 
 * @author ruoyi
 */
@Component
public class SysRegisterService
{
    @Autowired
    private ISysQualitygGroupService QualitygGroupService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ISysRoleService roleService;

    /**
     * 注册
     */
    public String register(RegisterBody registerBody)
    {
        String msg = "", QualitygGroupname = registerBody.getQualitygGroupname(), password = registerBody.getPassword();
        if (StringUtils.isEmpty(registerBody.getSex())){
            registerBody.setSex("2");
        }
        boolean captchaOnOff = configService.selectCaptchaOnOff();
        // 验证码开关
        if (captchaOnOff)
        {
            validateCaptcha(QualitygGroupname, registerBody.getCode(), registerBody.getUuid());
        }

        if (StringUtils.isEmpty(QualitygGroupname))
        {
            msg = "用户名不能为空";
        }
        else if (StringUtils.isEmpty(password))
        {
            msg = "用户密码不能为空";
        }
        else if (QualitygGroupname.length() < QualitygGroupConstants.QualitygGroupNAME_MIN_LENGTH
                || QualitygGroupname.length() > QualitygGroupConstants.QualitygGroupNAME_MAX_LENGTH)
        {
            msg = "账户长度必须在2到20个字符之间";
        }
        else if (password.length() < QualitygGroupConstants.PASSWORD_MIN_LENGTH
                || password.length() > QualitygGroupConstants.PASSWORD_MAX_LENGTH)
        {
            msg = "密码长度必须在5到20个字符之间";
        }
        else if (QualitygGroupConstants.NOT_UNIQUE.equals(QualitygGroupService.checkQualitygGroupNameUnique(QualitygGroupname)))
        {
            msg = "保存用户'" + QualitygGroupname + "'失败，注册账号已存在";
        }
        else
        {
            SysQualitygGroup sysQualitygGroup = new SysQualitygGroup();
            sysQualitygGroup.setQualitygGroupName(QualitygGroupname);
            sysQualitygGroup.setNickName(QualitygGroupname);
            sysQualitygGroup.setPhonenumber(registerBody.getPhone());
            sysQualitygGroup.setEmail(registerBody.getEmail());
            sysQualitygGroup.setSex(registerBody.getSex());
            sysQualitygGroup.setPassword(SecurityUtils.encryptPassword(registerBody.getPassword()));

            boolean regFlag = QualitygGroupService.registerQualitygGroup(sysQualitygGroup);
            if (!regFlag)
            {
                msg = "注册失败,请联系系统管理人员";
            }
            else
            {
                Long QualitygGroupId = sysQualitygGroup.getQualitygGroupId();
                Long[] id = {QualitygGroupId};
                roleService.insertAuthQualitygGroups(2L,id);
                AsyncManager.me().execute(AsyncFactory.recordLogininfor(QualitygGroupname, Constants.REGISTER,
                        MessageUtils.message("QualitygGroup.register.success")));
            }
        }
        return msg;
    }

    /**
     * 校验验证码
     * 
     * @param QualitygGroupname 用户名
     * @param code 验证码
     * @param uuid 唯一标识
     * @return 结果
     */
    public void validateCaptcha(String QualitygGroupname, String code, String uuid)
    {
        String verifyKey = Constants.CAPTCHA_CODE_KEY + StringUtils.nvl(uuid, "");
        String captcha = redisCache.getCacheObject(verifyKey);
        redisCache.deleteObject(verifyKey);
        if (captcha == null)
        {
            throw new CaptchaExpireException();
        }
        if (!code.equalsIgnoreCase(captcha))
        {
            throw new CaptchaException();
        }
    }
}
